import 'package:flutter/material.dart';
import 'package:flutter_fancy_tree_view/flutter_fancy_tree_view.dart';

import '../common/common.dart';
import '../app_controller.dart';

part '_actions_chip.dart';
part '_selector.dart';
part '_title.dart';

const Color _kDarkBlue = Colors.deepPurple;

const RoundedRectangleBorder kRoundedRectangleBorder = RoundedRectangleBorder(
  borderRadius: BorderRadius.all(Radius.circular(12)),
);

class TreeNodeTile extends StatefulWidget {
  const TreeNodeTile({Key? key}) : super(key: key);

  @override
  _TreeNodeTileState createState() => _TreeNodeTileState();
}

class _TreeNodeTileState extends State<TreeNodeTile> {
  @override
  Widget build(BuildContext context) {
    final appController = AppController.of(context);
    final nodeScope = TreeNodeScope.of(context);

    void _describeAncestors(context,TreeNode node, String type) {
      print(node);
      // final ancestors = node.ancestors.map((ancestor) => ancestor.id).join('/');
      if (type == 'tap') {
        showMessage(context, '您勾选/取消了“${node.label}”');
        appController.toggleSelection(node.id);
      } else if (type == 'longPress') {
        if (!appController.multiple) {
          showMessage(context, '当前选择器只支持单选');
          return;
        }
        showMessage(context, '您勾选/取消了“${node.label}”及所有下级');
        appController.longPress(node.id);
      }
    }

    return InkWell(
      onTap: () => _describeAncestors(context,nodeScope.node, 'tap'),
      onLongPress: () => _describeAncestors(context,nodeScope.node, 'longPress'),
      child: ValueListenableBuilder<ExpansionButtonType>(
        valueListenable: appController.expansionButtonType,
        builder: (context, ExpansionButtonType buttonType, __) {
          return Row(
            children: buttonType == ExpansionButtonType.folderFile
                ? const [
                    LinesWidget(),
                    NodeWidgetLeadingIcon(useFoldersOnly: true),
                    // _NodeActionsChip(),
                    _NodeSelector(),
                    SizedBox(width: 8),
                    Expanded(child: _NodeTitle()),
                  ]
                : [
                    LinesWidget(),
                    // SizedBox(width: 4),
                    // _NodeActionsChip(),
                    _NodeSelector(),
                    // SizedBox(width: 8),
                    Expanded(child: _NodeTitle()),
                    if (nodeScope.node.hasChildren)
                      ExpandNodeIcon(expandedColor: _kDarkBlue),
                  ],
          );
        },
      ),
    );
  }
}

void showMessage(context, String meg) {
  showSnackBar(
    context,
    meg,
    duration: const Duration(seconds: 2),
  );
}
